﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Provider;
using CE.CarInsurance;
using CE.Company;
using CE.Mail;
using CE.InputOut;
using CE.Image;

public partial class car_insurance_email_this : MyLanguageAndSessionBasePage
{
    string _redirectUrl = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.Page.Title = Resources.Title.EmailThisTitle;

        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.Name = "language";
        language.Content = Resources.Title.PageLanguage;

        HtmlMeta keywords = new HtmlMeta();
        keywords.Name = "keywords";
        keywords.Content = Resources.Title.EmailThisKeyword;

        HtmlMeta description = new HtmlMeta();
        description.Name = "description";
        description.Content = Resources.Title.EmailThisDescription;

        Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(11, language);
        Page.Header.Controls.AddAt(12, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(13, keywords);
        Page.Header.Controls.AddAt(14, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(15, description);

        if (Session[General.UserId] != null && Session[General.CarId] != null && Session[General.UserName] != null && Session[General.CarResultId] != null)
        {
            if (Cache["CarResult" + Session[General.UserId].ToString() + Session[General.CarId].ToString()] == null)
            {
                Response.Redirect("login.aspx");
            }
        }
        else
        {
            Response.Redirect("login.aspx");
        }
        if (!Page.IsPostBack)
        {
            txtFullName.Text = Session[General.UserName].ToString();
            txtEmail.Text = Session[General.UserEmail].ToString();
        }

    }

    protected void btnEmailThis_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (SendMailToFriend())
            {
                string _providerID = Session[CE.Global.General.ProviderCompanyId].ToString();
                string _ec = "";

                if (_providerID == "23")
                {
                    _ec = "1223";
                }
                else if (_providerID == "29")
                {
                    _ec = "1229";
                }
                else if (_providerID == "30")
                {
                    _ec = "1230";
                }

                CarUserOptionLog();

                _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/email-usermessage.aspx", Page);
                Response.Redirect(_redirectUrl);
            }
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        if (Session["FromSummary"] != null)
        {
            if (Session["FromSummary"].ToString().Trim() == "1")
            {
                _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/results.aspx", Page);
            }
            else
            {
                _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/results-details.aspx", Page);
            }
        }
        else
        {
            _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/results.aspx", Page);
        }

        Response.Redirect(_redirectUrl);
    }

    private bool SendMailToFriend()
    {
        CE.User.Profile usrprofile = new Profile();
        ProfileDB usrprofiledb = new ProfileDB();

        usrprofile = usrprofiledb.GetProfile(Convert.ToInt64(Session[General.UserId].ToString()));

        string userfirstname = "";

        if (usrprofile != null)
        {
            userfirstname = usrprofile.FirstName;
        }

        bool _retVal = false;
        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");

        string _emailTo = txtEmailTo.Text.Trim();
        string _customername = Session[General.UserName].ToString();
        string _emailSubject = Resources.EmailMessage.Subject;
        string _mailBody = "";
        string _currentDomainUrl = "http://www.gluaygluay.com";
        string imgLogoUrl = _currentDomainUrl + CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LogoPath");

        _mailBody = "<body class='email_body'>";
        _mailBody += "<style type='text/css'>";
        _mailBody += ".email_body {background:white;margin:0; padding:0; color:#4c4c4c; font-family:Arial, Helvetica, sans-serif; font-size:0.85em;}";
        _mailBody += "a {text-decoration:underline;color:#2c95cc;} ";
        _mailBody += "#email_container {margin:0 auto;padding:20px;width:600px; height:auto;} ";
        _mailBody += "#email_body {width:100%; text-align: left !important;} ";
        _mailBody += ".email_pad10px {padding:10px;} ";
        _mailBody += ".email_results td {width:33%;} ";
        _mailBody += "</style>";
        _mailBody += "<center>";

        _mailBody += "<table cellpadding='0' cellspacing='0' border='0' style='padding:20px;' width='600'>";
        _mailBody += "<tr><td width='360'><a href='http://www.gluaygluay.com/car-insurance/sign-in.aspx' target='_blank'><img src='" + _currentDomainUrl + "/images/email/header_quote_plain.png' width='596' height='187' alt='GluayGluay.com' border='0' /></a></td></tr>";
        _mailBody += "<tr><td style='padding-left:45px; padding-right:45px;'>";
        _mailBody += "<strong>" + Resources.EmailMessage.Dear + " " + txtFriendName.Text.Trim() + ",</strong><br/><br/>";
        _mailBody += userfirstname + " (" + Session[General.UserEmail].ToString() + ") ";
        _mailBody += Resources.EmailMessage.SentBy + "<br/><br/>";

        DataView dvResults = new DataView((DataTable)Cache["CarResult" + Session[General.UserId].ToString() + Session[General.CarId].ToString()]);

        _mailBody += "<table cellpadding='5' cellspacing='0' border='0' width='546px' class='table_details'>";

        string CITempResultId = Session[CE.Global.General.CarResultId].ToString();
        dvResults.RowFilter = " CITempResultId in (" + CITempResultId + ")";

        DataTable dtResults = dvResults.ToTable();


        if (dtResults.Rows.Count > 0)
        {
            string _providercompanyid = dtResults.Rows[0]["providercompanyid"].ToString();
            //logo
            string logourl = CE.Global.General.GetProviderCompanyLogo(_providercompanyid);

            _mailBody += "<tr>";
            _mailBody += "<td width='45%'></td>";
            _mailBody += "<td width='55%' align='center' style='background:#e0e0e0; border-bottom:white 1px dashed;'><img src='" + _currentDomainUrl + logourl + "' width='130' style='padding:3px;'/></td>";
            _mailBody += "</tr>";

            string insurername = dtResults.Rows[0]["PackageName"].ToString();

            //insurer name
            _mailBody += "<tr>";
            _mailBody += "<td><strong>" + Resources.CarInsuranceResultPage.Insurer + "</strong></td>";
            _mailBody += "<td align='center' style='background:#d5d5d5; color:white;'><strong>" + insurername + "</strong></td>";
            _mailBody += "</tr>";

            //premium
            _mailBody += "<tr>";
            _mailBody += "<td align='left' style='background:#d5d5d5' ><strong>" + Resources.CarInsuranceResultPage.TotalVMI + "</strong></td>";
            _mailBody += "<td align='center' style='background:#c5c5c5; color:black; font-size:20px;'>";

            if (Convert.ToSingle(dtResults.Rows[0]["annualpremium"]) != Convert.ToSingle(dtResults.Rows[0]["annualpremium"]))
            {
                _mailBody += "<s>" + String.Format("{0:0,0.00}", Convert.ToSingle(dtResults.Rows[0]["annualpremium"].ToString())) + "</s>";
            }

            _mailBody += "<br/><strong>" + String.Format("{0:0,0.00}", Convert.ToSingle(dtResults.Rows[0]["PromotionPrice"].ToString())) + "</strong>";
            _mailBody += "</td>";
            _mailBody += "</tr>";

            float _fMonthlyPremiumAmount = Convert.ToSingle(dtResults.Rows[0]["MonthlyPremiumAmount"].ToString());
            string monthlyInstallment = "";

            if (_fMonthlyPremiumAmount > 0.0f)
            {
                string _monthlyPremiumAmount = String.Format("{0:0,0.00}", _fMonthlyPremiumAmount);
                monthlyInstallment = _monthlyPremiumAmount + "/mth<br/>";
            }
            else
            {
                monthlyInstallment = "<img src='" + _currentDomainUrl + "/images/icon/X.png' width='16' alt='NA' />";
            }
            //Deductible  
            _mailBody += "<tr>";
            _mailBody += "<td style='border-bottom:1px solid #ccc;'><strong>" + Resources.CarInsuranceResultPage.DEC + "</strong></td>";
            float _fDEC = Convert.ToSingle(dtResults.Rows[0]["DEC"].ToString());
            string deductible = "";
            if (_fDEC > 0.0f)
            {
                deductible = String.Format("{0:0,0.00}", _fDEC);
            }
            else
            {
                deductible = Resources.CarInsuranceResultPage.Waived;  //"<img src='" + _currentDomainUrl + "/images/icon/X.png' width='16' alt='NA' />";
            }
            _mailBody += "<td align='center' style='background:#c5c5c5' >" + deductible + "</td>";
            _mailBody += "</tr>";

            string suminsured = String.Format("{0:#,0}", Convert.ToSingle(dtResults.Rows[0]["SumInsured"].ToString()));
            if (suminsured == "0")
            {
                suminsured = "<img src='" + _currentDomainUrl + "/images/icon/X.png' width='16' alt='NA' />";
            }
            //Own Damage
            _mailBody += "<tr>";
            _mailBody += "<td><strong>" + Resources.CarInsuranceResultPage.OwnDamage + "</strong></td>";
            _mailBody += "<td align='center' style='background:#d5d5d5' >" + suminsured + "</td>";
            _mailBody += "</tr>";

            //Fire
            _mailBody += "<tr>";
            _mailBody += "<td><strong>" + Resources.CarInsuranceResultPage.Fire + "</strong></td>";
            _mailBody += "<td align='center' style='background:#c5c5c5' >" + suminsured + "</td>";
            _mailBody += "</tr>";

            //Theft
            _mailBody += "<tr>";
            _mailBody += "<td><strong>" + Resources.CarInsuranceResultPage.Theft + "</strong></td>";
            _mailBody += "<td align='center' style='background:#d5d5d5' >" + suminsured + "</td>";
            _mailBody += "</tr>";

            //TPPD
            _mailBody += "<tr>";
            _mailBody += "<td><strong>" + Resources.CarInsuranceResultPage.TPPD + "</strong></td>";
            _mailBody += "<td align='center' style='background:#c5c5c5' >" + String.Format("{0:#,0}", Convert.ToSingle(dtResults.Rows[0]["TPPD"].ToString())) + "</td>";
            _mailBody += "</tr>";

            //TPBIA
            _mailBody += "<tr>";
            _mailBody += "<td style='border-bottom:1px solid #ccc;'><strong>" + Resources.CarInsuranceResultPage.TPBIA + "</strong></td>";
            _mailBody += "<td align='center' style='background:#d5d5d5' >" + String.Format("{0:#,0}", Convert.ToSingle(dtResults.Rows[0]["TPBIA"].ToString())) + "</td>";
            _mailBody += "</tr>";

            //TPBIP
            _mailBody += "<tr>";
            _mailBody += "<td style='border-bottom:1px solid #ccc;'><strong>" + Resources.CarInsuranceResultPage.TPBIP + "</strong></td>";
            _mailBody += "<td align='center' style='background:#c5c5c5' >" + String.Format("{0:#,0}", Convert.ToSingle(dtResults.Rows[0]["TPBIP"].ToString())) + "</td>";
            _mailBody += "</tr>";

            //PA Driver
            _mailBody += "<tr>";
            _mailBody += "<td style='border-bottom:1px solid #ccc;'><strong>" + Resources.CarInsuranceResultPage.PADriver + "</strong></td>";
            _mailBody += "<td align='center' style='background:#d5d5d5' >" + String.Format("{0:#,0}", Convert.ToSingle(dtResults.Rows[0]["PADriver"].ToString())) + "</td>";
            _mailBody += "</tr>";


            int _MedicalExpense = Convert.ToInt32(dtResults.Rows[0]["MedicalExpense"].ToString());
            string medicalExpense = "";

            if (_MedicalExpense == 0)
            {
                medicalExpense = "<img src='" + _currentDomainUrl + "/images/icon/X.png' width='16' alt='NA' />";
            }
            else
            {
                medicalExpense = String.Format("{0:#,0}", Convert.ToSingle(_MedicalExpense.ToString()));
            }

            //Medical Expense
            _mailBody += "<tr>";
            _mailBody += "<td><strong>" + Resources.CarInsuranceResultPage.MED + "</strong></td>";
            _mailBody += "<td align='center' style='background:#c5c5c5' >" + medicalExpense + "</td>";
            _mailBody += "</tr>";

            //BB
            _mailBody += "<tr>";
            _mailBody += "<td style='border-bottom:1px solid #ccc;'><strong>" + Resources.CarInsuranceResultPage.BailBond + "</strong></td>";
            _mailBody += "<td align='center' style='background:#d5d5d5' >" + String.Format("{0:#,0}", Convert.ToSingle(dtResults.Rows[0]["BailBond"].ToString())) + "</td>";
            _mailBody += "</tr>";

        }

        _mailBody += "</table>";

        _mailBody += "<hr />";
        _mailBody += Resources.EmailMessage.Footer;
        _mailBody += "<br/>";
        _mailBody += "<br /><i>" + Resources.EmailMessage.NoReply + "</i>";
        _mailBody += "<hr />";
        _mailBody += "</center></body>";

        if (MyMailManager.SendCARMessage(_emailTo, _emailSubject, _mailBody))
        {
            _retVal = true;
        }
        else
        {
            _retVal = false;
        }

        return _retVal;
    }

    private void CarUserOptionLog()
    {
        CarUserOption caruseroption = new CarUserOption();
        CarUserOptionDB caruseroptiondb = new CarUserOptionDB();

        caruseroption.CarUserOptionId = 0;
        caruseroption.CECarId = Convert.ToInt64(Session[General.CarId].ToString());
        caruseroption.IsBuyNow = false;
        caruseroption.IsCallMe = false;
        caruseroption.IsEmailMe = false;
        caruseroption.IsEmailThis = true;
        caruseroption.RecipientName = General.ClearScriptTag(txtFriendName.Text.Trim());
        caruseroption.RecipientEmailAddress = txtEmailTo.Text.Trim();
        caruseroption.ViewedDetailId = 0;
        caruseroption.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

        caruseroptiondb.Insert(caruseroption);
    }

}
